﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ExaminationSys.DataAccess.Entity;
using SQLinq;
using Dapper;
using SQLinq.Dapper;

using DapperExtensions;

namespace ExaminationSys.DataAccess.Repository
{
    public class LoginLogRepository : Repository<LoginLog>
    {
        public LoginLogRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }

        private SqlConnection db;

        public override LoginLog GetById(dynamic id)
        {
            return base.ObjectSet.Get<LoginLog>(db, id, null, null);
        }
    }

    public class LoginLogService
    {
        private LoginLogService()
        {
        }

        public LoginLog Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                //另外一种调用方式
                //var result = db.Query(from p in new SQLinq<News>()
                //                      where p.ID == id
                //                      select p).FirstOrDefault();
                //return result;
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.LoginLog.GetById(id);
                return result;

            }
        }
        public LoginLog GetLoginLogByUserId(string userId,int loginType)
        {
            using (var db = DataContent.DBServer())
            {
                var result =
                    db.Query<LoginLog>("select top 1 * from LoginLog  where UserID='" + userId +
                                       "' and logintype="+loginType+" order by LoginTime desc").
                        FirstOrDefault();
                return result;
            }
        }

        public LoginLog GetLoginSection(string sectionId,string userid)
        {
            using (var db = DataContent.DBServer())
            {
                var result =
                    db.Query<LoginLog>(@"SELECT top 1 * FROM LoginLog ll WHERE ll.LoginType=2 AND ll.LastResourceID='" +
                                       sectionId + "' and UserID='" + userid + "' ORDER BY ll.LoginTime DESC").FirstOrDefault();
                return result ;
            }
        }

        public int GetLoginCount(string userId, int loginType)
        {
            using (var db = DataContent.DBServer())
            {
                var sql = string.Format("select count(1) as Count from LoginLog  where UserID='{0}' and loginType={1}", userId,
                                        loginType);
                var result = db.Query(sql).FirstOrDefault();
                return result == null ? 0 : (int)result.Count;
            }
        }

        public dynamic Insert(LoginLog entity)
        {

            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.LoginLog.Insert(entity, null);
            }
        }
    }
}
